package cn.springcloud.fix.gateway.service.impl;

import cn.springcloud.fix.common.core.base.R;
import cn.springcloud.fix.gateway.dao.entity.GatewayRouteVersion;
import cn.springcloud.fix.gateway.dao.mapper.GatewayRouteVersionMapper;
import cn.springcloud.fix.gateway.service.VersionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Slf4j
@Service
@AllArgsConstructor
public class VersionServiceImpl implements VersionService {

    private GatewayRouteVersionMapper gatewayRouteVersionMapper;

    @Override
    public R<Long> getLastVersion() {
        QueryWrapper<GatewayRouteVersion> wrapper = new QueryWrapper<>();
        wrapper.select("max(id) as id");
        GatewayRouteVersion version = gatewayRouteVersionMapper.selectOne(wrapper);
        Long id = Optional.ofNullable(version).map(GatewayRouteVersion::getId).orElse(null);
        return R.SUCCESS(id);
    }

    @Override
    public int addVersion() {
        GatewayRouteVersion version = new GatewayRouteVersion();
        // LocalDateTime localDateTime = LocalDateTime.now();
        // version.setCreateTime(localDateTime);
        // version.setUpdateTime(localDateTime);
        return gatewayRouteVersionMapper.insert(version);
    }
}
